//
//  helpMeViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "helpMeViewController.h"
#import "helpMeModel.h"

static NSString *cellPath = @"helpCell";
@interface helpMeViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *allHelpAry;

@end

@implementation helpMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestHelpAll];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"帮助中心";
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SCREER_HeiX-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = BacksColor;
    [_tableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [self.view addSubview:_tableview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _allHelpAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    helpMeModel *model = _allHelpAry[section];
    if (model.isShow)
    {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_allHelpAry.count > 0)
    {
        helpMeModel *model = _allHelpAry[indexPath.section];
        if (indexPath.row == 0)
        {
            cell.textLabel.text = model.title;
            cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
            cell.textLabel.textColor = RGB(77, 77, 77);
            NSString *imgStr = @"xiangxia";
            if (model.isShow)
            {
                imgStr = @"xiangshang";
            }
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-35*displaywidth, 15*displaywidth, 20*displaywidth, 15*displaywidth) ImageName:imgStr];
            imgView.contentMode = UIViewContentModeScaleAspectFit;
            [cell.contentView addSubview:imgView];
        }
        else
        {
            [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            CGFloat H = 30*displaywidth;
            if (model.cellHeight > 0)
            {
                H = model.cellHeight+15*displaywidth;
            }
            UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, SCREEN_WIDTH-30*displaywidth, H)];
            webView.delegate = self;
            webView.tag = 100+indexPath.section;
            webView.scrollView.scrollEnabled = NO;
            //预先加载url
            [webView loadHTMLString:model.content baseURL:nil];
            [cell.contentView addSubview:webView];
        }
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        helpMeModel *model = _allHelpAry[indexPath.section];
        model.isShow = !model.isShow;
        [tableView reloadData];
//        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:indexPath.section];
//        [tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    NSInteger index = webView.tag-100;
    helpMeModel *model = _allHelpAry[index];
    if (model.cellHeight > 0)
    {
        return;
    }
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    if (height < 30*displaywidth)
    {
        height = 30*displaywidth;
    }
    model.cellHeight = height;
    [self.tableview reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    helpMeModel *model = _allHelpAry[indexPath.section];
    if (indexPath.row == 0)
    {
        return 50*displaywidth;
    }
    else
    {
        if (model.isShow)
        {
            if (model.cellHeight > 0)
            {
                return model.cellHeight+35*displaywidth;
            }
            else
            {
                return 50*displaywidth;
            }
        }
        return 0;
    }
    
    return 1;
}

- (void)requestHelpAll
{
    WEAKSELF;
    [HYBaseHandler requestHelpAllParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.allHelpAry = [helpMeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
